# -*- coding: utf8 -*-

from sqlalchemy import create_engine, __version__
from sqlalchemy.orm import sessionmaker

class DataBase(object):
    
    db = None
    
    engine = None

    def init( self, db = {} ):
        self.db = {
            "dialect" : "sqlite",
            "connection" : "",
            "database" : ":memory:",
            "options" : ""
        }

        print("DataBase: sqlalchemy=%r" % (__version__,) )

        self.db.update(db)
        
        # print("connect uri = %(dialect)s://%(connection)s/%(database)s%(options)s" % self.db)

        self.engine = None
        self.Session = None
        pass

    def getEngine(self):
        if not self.engine:
            self.engine = create_engine("%(dialect)s://%(connection)s/%(database)s%(options)s" % self.db,
                pool_size=16, pool_recycle=30,
                convert_unicode=True,
                # echo=True,
                # echo_pool=True,
                # strategy='threadlocal'
            )
        return self.engine

    def getSession(self):
        if not self.Session:
            # self.Session = scoped_session( sessionmaker(bind=self.getEngine()) )
            self.Session = sessionmaker(bind=self.getEngine())
        return self.Session()


database = DataBase()
